# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :w_model.py
# @Time      :2025/7/18 11:20
# @Author    :aiyimu

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.font_manager import FontProperties

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 创建画布
fig, ax = plt.subplots(figsize=(14, 10))

# ========== 第一个V：开发流程 ==========
dev_nodes = {
    "用户需求": (-4, 4),
    "需求分析": (-4, 3),
    "概要设计": (-4, 2),
    "详细设计": (-4, 1),
    "编码": (-4, 0),  # 第一个V顶点
    "交付": (-2, -1),
    "实施": (0, -2),
    "集成": (2, -3)
}

# 绘制开发V的连接线（蓝色）
dev_flow = ["用户需求", "需求分析", "概要设计", "详细设计", "编码", "交付", "实施", "集成"]
for i in range(len(dev_flow) - 1):
    x1, y1 = dev_nodes[dev_flow[i]]
    x2, y2 = dev_nodes[dev_flow[i + 1]]
    ax.plot([x1, x2], [y1, y2], 'b-', linewidth=2)

# ========== 第二个V：测试流程 ==========
test_nodes = {
    "验收准备": (4, 4),
    "系统测试准备": (4, 3),
    "集成测试准备": (4, 2),
    "单元测试准备": (4, 1),
    "单元测试": (4, 0),  # 第二个V顶点
    "集成测试": (2, -1),
    "系统测试": (0, -2),
    "验收测试": (-2, -3)
}

# 绘制测试V的连接线（红色）
test_flow = ["验收准备", "系统测试准备", "集成测试准备", "单元测试准备", "单元测试", "集成测试", "系统测试", "验收测试"]
for i in range(len(test_flow) - 1):
    x1, y1 = test_nodes[test_flow[i]]
    x2, y2 = test_nodes[test_flow[i + 1]]
    ax.plot([x1, x2], [y1, y2], 'r-', linewidth=2)

# ========== 连接两个V形成W ==========
# 开发与测试的对应关系（虚线）
connections = [
    ("用户需求", "验收准备", "gray"),
    ("需求分析", "系统测试准备", "green"),
    ("概要设计", "集成测试准备", "purple"),
    ("详细设计", "单元测试准备", "orange"),
    ("编码", "单元测试", "blue"),
    ("交付", "集成测试", "cyan"),
    ("实施", "系统测试", "brown"),
    ("集成", "验收测试", "pink")
]

for src, dst, color in connections:
    if src in dev_nodes and dst in test_nodes:
        x1, y1 = dev_nodes[src]
        x2, y2 = test_nodes[dst]
        ax.plot([x1, x2], [y1, y2], linestyle='--', color=color, alpha=0.6)

# ========== 绘制所有节点 ==========
def draw_nodes(nodes, color):
    for name, (x, y) in nodes.items():
        ax.add_patch(patches.Circle((x, y), radius=0.3, facecolor=color, edgecolor='black'))
        ax.text(x, y, name, ha='center', va='center', fontsize=10)

draw_nodes(dev_nodes, 'skyblue')  # 开发节点
draw_nodes(test_nodes, 'lightcoral')  # 测试节点

# ========== 添加标注和标题 ==========
ax.set_title("W模型（开发与测试双V并行）", fontsize=16, pad=20)
ax.text(-5, 4.5, "开发V", color='blue', fontsize=12)
ax.text(3, 4.5, "测试V", color='red', fontsize=12)

# 隐藏坐标轴
ax.set_xlim(-6, 6)
ax.set_ylim(-4, 5)
ax.axis('off')

plt.tight_layout()
plt.savefig("w_model_combined.png", dpi=300, bbox_inches='tight')
plt.show()